/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.entity.BusinessObjectives;
import org.springblade.modules.restaurant.service.IBusinessObjectivesService;
import org.springblade.modules.restaurant.vo.BusinessObjectivesVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 营业目标表 控制器
 *
 * @author Blade
 * @since 2025-02-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/businessobjectives")
@Tag(name = "营业目标表", description = "营业目标表接口")
public class BusinessObjectivesController extends BladeController {

	private IBusinessObjectivesService businessObjectivesService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入businessObjectives")
	public R<BusinessObjectives> detail(BusinessObjectives businessObjectives) {
		BusinessObjectives detail = businessObjectivesService.getOne(Condition.getQueryWrapper(businessObjectives));
		return R.data(detail);
	}

	/**
	 * 分页 营业目标表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入businessObjectives")
	public R<IPage<BusinessObjectives>> list(BusinessObjectives businessObjectives, Query query) {
		LambdaQueryWrapper<BusinessObjectives> wrapper = Wrappers.<BusinessObjectives>lambdaQuery()
			.eq(ObjectUtil.isNotNull(businessObjectives.getBusinessTime()), BusinessObjectives::getBusinessTime, businessObjectives.getBusinessTime())
			.orderByDesc(BusinessObjectives::getBusinessTime);
		IPage<BusinessObjectives> pages = businessObjectivesService.page(Condition.getPage(query), wrapper);
		return R.data(pages);
	}

	/**
	 * 自定义分页 营业目标表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入businessObjectives")
	public R<IPage<BusinessObjectivesVO>> page(BusinessObjectivesVO businessObjectives, Query query) {
		IPage<BusinessObjectivesVO> pages = businessObjectivesService.selectBusinessObjectivesPage(Condition.getPage(query), businessObjectives);
		return R.data(pages);
	}

	/**
	 * 新增 营业目标表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入businessObjectives")
	public R save(@Valid @RequestBody BusinessObjectives businessObjectives) {
		// 判断当天是否已经有数据
		BusinessObjectives one = businessObjectivesService.getOne(Wrappers.<BusinessObjectives>lambdaQuery()
			.eq(BusinessObjectives::getBusinessTime, businessObjectives.getBusinessTime()));
		if (one != null) {
			return R.fail(one.getBusinessTime() + " 数据已存在");
		}
		return R.status(businessObjectivesService.save(businessObjectives));
	}

	/**
	 * 修改 营业目标表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入businessObjectives")
	public R update(@Valid @RequestBody BusinessObjectives businessObjectives) {
		return R.status(businessObjectivesService.updateById(businessObjectives));
	}

	/**
	 * 新增或修改 营业目标表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入businessObjectives")
	public R submit(@Valid @RequestBody BusinessObjectives businessObjectives) {
		return R.status(businessObjectivesService.saveOrUpdate(businessObjectives));
	}


	/**
	 * 删除 营业目标表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(businessObjectivesService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 重新计算
	 */
	@PostMapping("/calculate")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "重新计算", description = "传入ids")
	public R calculate(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		List<BusinessObjectives> list = businessObjectivesService.listByIds(Func.toLongList(ids));
		for (BusinessObjectives businessObjectives : list) {
			String businessTime = businessObjectives.getBusinessTime();
			String tenantId = businessObjectives.getTenantId();

			String time = DateUtil.formatDate(DateUtil.endOfMonth(DateUtil.parse(businessTime, "yyyy-MM")));
			// 获取当前月份
			String now = DateUtil.format(DateUtil.date(), "yyyy-MM");
			if (businessTime.equals(now)) {
				time = DateUtil.formatDate(DateUtil.date());
			}
			businessObjectivesService.calculate(businessObjectives, time, tenantId);
		}
		return R.success("计算成功");
	}
}
